// const fs = require('fs');
import thenFs from 'then-fs'

// 基于 Promise 方式读取文件内容

// 注意 .then() 中的失败函数是可选的, 可以被省略
thenFs.readFile('./files/1.txt').then((r1) => {
  console.log(r1.toString())
}, (err1) => {
  console.log(err1.message)
})
thenFs.readFile('./files/2.txt').then((r2) => {
  console.log(r2.toString())
}, (err2) => {
  console.log(err2.message)
})

// ENOENT: no such file or directory, open 'D:\WebstormProjects\js-learn\node\files\4.txt'
/*thenFs.readFile('./files/4.txt').then((r3) => {
  console.log(r3.toString())
}, (err3) => {
  console.log(err3.message)
})*/

// ENOENT: no such file or directory, open 'D:\WebstormProjects\js-learn\node\files\4.txt'
thenFs.readFile('./files/3.txt').then((r3) => {
  console.log(r3.toString())
}).catch((err3) => {
  console.log(err3.message)
})

// then_err
// ENOENT: no such file or directory, open 'D:\WebstormProjects\js-learn\node\files\4.txt'
/*thenFs.readFile('./files/4.txt').then((r3) => {
  console.log(r3.toString())
}, (err3) => {
  console.log('then_err')
  console.log(err3.message)
}).catch((catchErr) => {
  console.log('catch_err')
  console.log(catchErr.message)
})*/

